<?php
/*    This file is part of Nephite Exchange.
 *
 *    Nephite Exchange is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    Nephite Exchange is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with Nephite Exchange.  If not, see <http://www.gnu.org/licenses/>.
 */
class Default_Model_ExchangeRate
{
	protected $_country;
	protected $_currencyName;
	protected $_ISOName;
	protected $_exchange;
	
	public $numberOfUnits = 8;
	protected $_units = array (
		"lo" => 7, 
		"se" => 4, 
		"sa" => 2, 
		"a" => 1.5, 
		"ss" => 1, 
		"so" => 0.5, 
		"su" => 0.25, 
		"l" => 0.125
	);
	
	public function getCountry () {
		return $this->_country;
	}

	public function setCountry ($country) {
		$this->_country = (string) $country;
		return $this;
	}

	public function getCurrencyName () {
		return $this->_currencyName;
	}

	public function setCurrencyName ($currencyName) {
		$this->_currencyName = (string) $currencyName;
		return $this;
	}

	public function getISOName () {
		return $this->_ISOName;
	}

	public function setISOName ($ISOName) {
		$this->_ISOName = (string) $ISOName;
		return $this;
	}

	public function getExchange () {
		return $this->_exchange;
	}

	public function setExchange ($exchange) {
		$this->_exchange = (string) $exchange;
		return $this;
	}

	/* This will only return the calculated exchange for the price. If they want to know how much
	 * X USD would translate to, that should be done at convertPriceToArray. This calculation always returns the
	 * amount in senum silver or senine gold units.
	 */
	public function getNephiteExchange ($barleyValue) {
		if ($this->_exchange === null) {
			return null;
		}

		/* Get the price of one liter of barley in USD. */
		$price1LBarley = $barleyValue / 1176.47;

		/* Multiply the USD value with the current exchange rate. */
		return 1 / ($price1LBarley * $this->_exchange);

	}

	public function convertPriceToArray ($amount, $barleyPrice) {
		$exchange = $this->getNephiteExchange($barleyPrice);
		
		if ($exchange === null || $exchange <= 0) {
			return -1;
		}
		
		$converted = $amount * $exchange;
		
		/* This is what we'll return with the breakdown of X per unit. */
		$breakdown = array ();
		
		foreach ($this->_units as $name => $value) {
			if (((int) ($converted / $value)) > 0) {
				/* Get the multiplier for the current unit. */
				$breakdown[$name] = (int) ($converted / $value);
				
				/* Subtract the multipler * current unit from the converted amount. */
				$converted = $converted - ($breakdown[$name] * $value);
			}
		}
		
		return $breakdown;
	}
}